package com.zkx.blogweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkx.blogweb.entity.IdArticle;
import com.zkx.blogweb.entity.User;
import com.zkx.blogweb.mapper.IdArticleMapper;
import com.zkx.blogweb.mapper.service.BlogServiceImp;
import com.zkx.blogweb.mapper.service.IdArticleServiceImp;
import com.zkx.blogweb.mapper.service.UserMapperImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zkx
 */
@Controller
@Slf4j
public class PassagesController {

    @Autowired
    IdArticleMapper idArticleMapper;

    @Autowired
    UserMapperImp userMapperImp;

    @Autowired
    IdArticleServiceImp idArticleServiceImp;

    @Autowired
    BlogServiceImp blogServiceImp;

    /**
     * 前端发送ajax请求得到数据
     * @param pageNo
     * @return
     */
    @RequestMapping("/pages/{pn}")
    public PageInfo<IdArticle> index(@PathVariable(value = "pn") int pageNo) {
        PageHelper.startPage(pageNo,5);
        List<IdArticle> list = idArticleMapper.getPageIdArticleList(pageNo,5);
        PageInfo<IdArticle> idArticlePageInfo = new PageInfo<>(list);
        return idArticlePageInfo;
    }

    /**
     * 根据文章id显示文章信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/article",method = {RequestMethod.POST,RequestMethod.GET})
    public String show_article(@RequestParam("id") Integer id,Model  model){
        IdArticle article = idArticleMapper.selectByPrimaryKey(id);
        Integer articleAuthorId = article.getArticleAuthorId();
        User checkUser = userMapperImp.selectByUserId(articleAuthorId);
        model.addAttribute("check_user",checkUser);
        model.addAttribute("article",article);
        return "article_show";
    }

    @ResponseBody
    @RequestMapping(value = "/search")
    public Integer searchBloger(Integer searchBlogId){
        IdArticle idArticle = idArticleServiceImp.selectUserByBlogId(searchBlogId);
        if(idArticle==null){
            return -1;
        }
        return searchBlogId;
    }


    /**
     * 根据id,显示作者信息
     * @param model
     * @param
     * @return
     */
    @RequestMapping(value = "/bloger",method = {RequestMethod.POST,RequestMethod.GET})
    public String toBloger(Model model,@RequestParam("id")Integer userId){
        log.info("userId={}",userId);
        User checkBloger = userMapperImp.selectByUserId(userId);
        model.addAttribute("check_user",checkBloger);
        //获取这个人的所有博文数
        Integer blogs = idArticleServiceImp.selectBlogsById(userId);
        model.addAttribute("blogs",blogs);
        //总赞数
        Integer stars = idArticleServiceImp.selectStarsById(userId);
        model.addAttribute("stars",stars);
        String userName = checkBloger.getUserName();
        model.addAttribute("userName",userName);
        return "check";

    }
    /**
     * 前端发送ajax请求，刷新页面
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/blogPage/{id}",method = {RequestMethod.POST,RequestMethod.GET})
    public PageInfo<IdArticle> getPage(@PathVariable("id") Integer id,Integer userId){
        log.info("id={}",userId);
        PageInfo<IdArticle> pageInfo = idArticleServiceImp.getPageInfoByBlogId(userId,id, 10);
        return pageInfo;
    }

    @RequestMapping(value = "/otherBlog",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String myBlogDesc(Integer userBlog){
        return blogServiceImp.myBlog(userBlog);
    }

    /**
     * 删除博客功能
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String deleteBlog(Integer blogId, Integer userId){

            //根据博客号查询返回
        IdArticle articleByBlogId = idArticleServiceImp.getArticleByBlogId(blogId);
        if(articleByBlogId==null){
            return "没有该文章";
        }
        int articleAuthorId = articleByBlogId.getArticleAuthorId();
        if(articleAuthorId != userId) return "对不起，你没有删除该博客的权限。";

        //删除业务逻辑
        idArticleServiceImp.deleteBlogById(blogId);
        return "success";
    }

    /**
     * 文章发表功能跳转页面
     * @return
     */
    @RequestMapping(value = "/articleUp",method = {RequestMethod.POST,RequestMethod.GET})
    public String articleUp(){
        return "article_up";
    }
    /**
     * 内容上传
     */
    @Transactional
    @ResponseBody
    @RequestMapping(value = "upPassage",method = {RequestMethod.POST,RequestMethod.GET})
    public String upToContext(Integer userId,String header,String context){
        IdArticle idArticle=  new IdArticle();
        idArticle.setContext(context);
        idArticle.setArticleAuthorId(userId);
        idArticle.setHeader(header);
        idArticle.setStars(0);
        System.out.println(idArticle);
        idArticleServiceImp.insertIdArtilce(idArticle);
        return "成功";
    }
}
